<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/12/18
 * Time: 15:56
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Core\Configure;

class GComponent extends Component
{

    public function error($msg = null, $memo = null)
    {
        $this->msg(400, $msg, $memo);
    }

    public function success($msg = null, $memo = null)
    {
        $this->msg(200, $msg, $memo);
    }

    private function msg($status = 400, $msg = null, $memo = null)
    {
        $controller = $this->_registry->getController();
        $controller->set('data', array(
            'status' => $status,
            'msg' => $msg,
            'memo' => $memo
        ));
    }

    // 设定http地址
    public function getImgUrl($path = null, $flag = null){
        if(is_numeric(strpos($path,'//'))){
            return $path;
        }
        $website = Configure::read('Website');
        $mainPath = $website['imgUrl'];
        $mainPath = empty($flag) ? '':$mainPath;
        $pathUrl = empty($path) ? 'nophoto.png' : $path;
        return $mainPath.$pathUrl;
    }

    // 干掉http地址
    public function setImgUrl($path = null){
        $website = Configure::read('Website');
        $mainPath = $website['imgUrl'];
        if(is_numeric(strpos($path,$mainPath))){
            $arr = explode('/',$path);
            unset($arr[0]);
            unset($arr[1]);
            unset($arr[2]);
            $str = implode('/',$arr);
            return $str;
        }
        return $path;
    }

    public function OverTime($time = null){
        return ($time + 1296000) > time() ? 0 : 1;
    }

}